package com.zy.web.demo.duoxiancheng.demo22;

/**
 * @author <a href="mailto:zhouyong@cqyyt.com">zy</a>
 * @version 1.0
 * @date 2020/8/21 15:11
 */
public class ShengChan {

    public static final int INT = 10;
    private String lock;
    private Integer num;

    public ShengChan(String lock,Integer num){
        this.lock = lock;
        this.num = num;
    }

    public void add(){
        synchronized (lock){
            while ( num == INT ){
                try {
                    System.out.println("~~~~~~~~~~~");
                    System.out.println("线程"+Thread.currentThread().getName()+"已满等待");
                    lock.wait();
                    System.out.println("线程"+Thread.currentThread().getName()+"被唤醒");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            num++;
            System.out.println("线程"+Thread.currentThread().getName()+",生产的数量是"+num);
            lock.notifyAll();
        }
    }

    public void del(){
        synchronized (lock){
            while ( num == 0 ){
                try {
                    System.out.println("线程"+Thread.currentThread().getName()+"库存已经消费完，等待");
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            num--;
            System.out.println("线程"+Thread.currentThread().getName()+",消费的数量是"+num);
            lock.notifyAll();
        }
    }


}
